layui.use(['jquery','element','table'],function() {
    var $ = layui.$, util = layui.util, element = layui.element, page = layui.laypage;

    /**
     * 订单管理初始化
     */
    var Order = {
        id: "OrderTable",	//表格id
        seItem: null,		//选中的条目
        table: null,
        layerIndex: -1
    };
    /**
     * 初始化layui分页
     */

    Order.initLayPage = function (state) {
       if(!state){
           state = ''
       }
        var pageConf = {
            limit: 10,
            offset: 1,
            state:state
        };
        $.post("/order/list", pageConf, function (data) {

            page.render({
                elem: 'laypage',
                count: data.total,
                curr: pageConf.offset,
                limit: pageConf.limit,
                first: "首页",
                last: "尾页",
                layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                jump: function (obj, first) {
                    if (!first) {
                        pageConf.offset = (obj.curr-1)*10;
                        pageConf.limit = obj.limit;
                        $.post("/order/list", pageConf, function (data) {
                            Order.fillTable(data.rows);
                        });

                    }
                }
            });
            Order.fillTable(data.rows);
        });

    }
    Order.operation = function (obj) {
        var state = obj.state, id = obj.id, remindnum = obj.remindnum ==undefined ? 0 : obj.remindnum;
        switch (state) {
            case '待付款':
                return '<span class="dele-btn" data-operation="pay"  onclick="Order1.onClickOpt(1,\''+id+'\','+remindnum+')">付款</span>\n';
                break;
            case '待发货':
                return '<span class="dele-btn" data-operation="remind" onclick="Order1.onClickOpt(2,\''+id+'\','+remindnum+')">提醒发货</span>\n';
                break;
            case '待收货':
                return '<span class="dele-btn" data-operation="confirm" onclick="Order1.onClickOpt(3,\''+id+'\','+remindnum+')">确认收货</span>\n';
                break;
            case '待评价':
                return '<span class="dele-btn" data-operation="evaluate" onclick="Order1.onClickOpt(4,\''+id+'\','+remindnum+')">评价</span>\n';
                break;
            default:
                return ''
        }
    }

    Order.reinitIframe = function(){
        var J_iframe = self.parent.document.getElementsByClassName("J_iframe")[0];
        try{
            var bHeight = J_iframe.contentWindow.document.body.scrollHeight;
            var dHeight = J_iframe.contentWindow.document.documentElement.scrollHeight;
            var height = Math.max(bHeight, dHeight);
            J_iframe.height = height;
        }catch (ex){
            console.log(ex)
        }
    }
//填充表格数据
    Order.fillTable = function (data) {
        $("#list-cont").empty();
        $.each(data, function (index, obj) {
            var orderMerStr = '';
            orderMerStr += '<ul class="item-content layui-clear">\n'+
                '                <li class="th th-id">\n' +
                '                <span class="id" >' + obj.id + '</span>\n' +
                '                </li>\n';
            var data1 = obj.orderMerList;
            var mertable = '<table style="float:left"><tbody>'
            $.each(data1, function (index, items) {
                var attr = items.merattr, merattrStr = '';
                $.each(items.merattr, function (index, item) {
                    merattrStr += '<span>' + item + '</span>&nbsp&nbsp\n'
                })
                mertable += '<tr><td><li class="th th-item">\n' +
                    '                <div class="item-cont">\n' +
                    '                <a href="javascript:;"><img src="/kaptcha/' + items.merpictureurl + '" alt=""></a>\n' +
                    '                <div class="text">\n' +
                    '                <div class="title">' + items.mername + '</div>\n' +
                    '                <p>\n' + merattrStr +
                    '            </div>\n' +
                    '            </div>\n' +
                    '            </li>\n</td>' +
                    '            <td><li class="th th-price-order">\n' +
                    '                <span class="th-su">' + items.merprice + '</span>\n' +
                    '                </li>\n</td>' +
                    '                <td><li class="th th-amount-order">\n' + items.merquantity +
                    '                </li>\n</td>';

            });
            mertable += '</tbody></table>';
            orderMerStr += mertable;
            orderMerStr += '<li class="th th-sum">\n' +
                '                <span class="sum">' + obj.sumprice + '</span>\n' +
                '                </li>\n' +
                '                <li class="th th-state">\n' +
                '                <span class="state" >' + obj.state + '</span>\n' +
                '                </li>\n' +
                '                <li class="th th-op">\n' + Order.operation(obj) +
                '                </li>\n' +
                '                </ul>';
            $("#list-cont").append(orderMerStr);

        });
        Order.reinitIframe();
    }


    /**
     * 打开查看订单详情
     */
    Order.openOrderDetail = function () {
        if (this.check()) {
            var index = layer.open({
                type: 2,
                title: '订单详情',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/order/order_update/' + Order.seItem.id
            });
            this.layerIndex = index;
        }
    };

    /**
     * 删除订单
     */
    Order.delete = function () {
        if (this.check()) {
            var ajax = new $ax(Feng.ctxPath + "/order/delete", function (data) {
                Feng.success("删除成功!");
                Order.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", this.seItem.id);
            ajax.start();
        }
    };

    Order.initLayPage();
    $(".layui-breadcrumb a").on('click',function(){
        var state = $(this).data('state');
        Order.initLayPage(state);
    })


})
var Order1 = {
};
Order1.addEvaluate=function(id) {
    var index = layer.open({
        type: 2,
        title: '评价',
        offset:window.parent.scrollY,
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/order/evaluate/'+ id
    });
    this.layerIndex = index;
};
Order1.onClickOpt = function(flag,id,remindnum){
    var path = '/order/update';
    var message = '';
    var order={
        id:id
    }
    switch(flag){
        case 1://付款
            order.state = '65';
            window.location.href='/order/pay?orderid='+id;
            break;
        case 2://提醒发货
            order.state = '65';
            order.remindnum = remindnum + 1 ;
            //提醒发货
            message = '提醒成功';
            break;
        case 3://确认发货
            order.state = '67';
            //收货过度窗口
            message = '收货成功';
            break;
        case 4://评价
            order.state = '68';
            message = '评价成功';
            this.addEvaluate(id);
            break;
    }
    if(flag==2 || flag==3){
        var ajax = new $ax(path, function (data) {
            Feng.success(message);
            window.location.reload();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set(order,flag);
        ajax.start();
    }


}
